#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Forum_Board
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Boards")

        If Me.Page.IsPostBack = False Then

            'Set Default Sort Order and Direction
            Me.sortCriteria = "sortorder"
            Me.sortDir = "asc"

            LoadPage()
        End If

        ConfigBaseNavCtrls()

        If Me.Page.IsPostBack = False Then
            BindDataItems()
        End If

    End Sub

#End Region

#Region " Save Data "

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New BoardList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetBoards(BoardList.BoardListType.publiclist)

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxBoardRepeater
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub LoadCategories(ByVal sender As Object, ByVal e As CommandEventArgs)
        Response.Redirect(String.Format("{1}.aspx?pid=for_category&categoryid={0}", e.CommandArgument.ToString, AppPage))
    End Sub

    Protected Sub uxBoardRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxBoardRepeater.ItemDataBound
        Dim item As RepeaterItem = e.Item
        Dim CategoryRepeater As Repeater = CType(item.FindControl("uxCategoryRepeater"), Repeater)
        Dim Category As New CategoryList
        Dim DT As New DataTable
        Dim DV As New DataView

        If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then

            If ShowItem(ConvertToGuid(e.Item.DataItem("BoardID").ToString)) = True Then
                DT = Category.GetCategoriesByBoard(New Guid(e.Item.DataItem("BoardID").ToString))

                DV.Table = DT

                DV.Sort = Me.sortCriteria & " " & Me.sortDir

                CategoryRepeater.DataSource = DV
                CategoryRepeater.DataBind()
            Else
                e.Item.Visible = False
            End If

        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

#End Region

End Class
